package com.puzzle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class SelectPicture extends Activity implements OnClickListener {

	private static int DEFAULT_SELECTED = 0;
	int pictureResource[] = { R.drawable.pic_one, R.drawable.pic_two,
			R.drawable.pic_three, R.drawable.pic_four, R.drawable.pic_five,
			R.drawable.pic_six, R.drawable.pic_seven, R.drawable.pic_eight };
	int selectId = DEFAULT_SELECTED;

	ImageView picture;
	ImageView back;
	ImageView done;
	ImageView prev;
	ImageView next;

	TextView scoreInfo;

	ScoreList scores;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_picture);

		picture = (ImageView) findViewById(R.id.picture);
		back = (ImageView) findViewById(R.id.back);
		next = (ImageView) findViewById(R.id.next);
		prev = (ImageView) findViewById(R.id.prev);
		done = (ImageView) findViewById(R.id.done);

		picture.setOnClickListener(this);
		back.setOnClickListener(this);
		next.setOnClickListener(this);
		prev.setOnClickListener(this);
		done.setOnClickListener(this);

		scoreInfo = (TextView) findViewById(R.id.score_view);

		scores = new ScoreList();
		showScoreInfo();
	}

	public void showScoreInfo() {
		Log.i("get score info", "get score");
		Score score = scores.getScoreInfoById(pictureResource[selectId]);

		String message = "no score";
		if (score != null) {
			message = "Player: " + score.name + "\n";
			message += "Time: " + convertSecondToMinute(score.time);
		}
		scoreInfo.setText(message);

	}
	public String convertSecondToMinute(int time){
		int minute = time /60;
		int second = time - minute * 60;
		
		String sMinute = String.valueOf(minute);
		if(minute < 10){
			sMinute = "0"+sMinute;
		}
		String sSecond = String.valueOf(second);
		if(second < 10){
			sSecond = "0"+sSecond;
		}
		
		return sMinute+":"+sSecond;
	}
	public void next() {
		if (selectId < pictureResource.length - 1) {
			selectId++;
		} else
			selectId = 0;

		picture.setImageResource(pictureResource[selectId]);
		showScoreInfo();
	}

	public void prev() {
		if (selectId > 0) {
			selectId--;
		} else
			selectId = pictureResource.length - 1;

		picture.setImageResource(pictureResource[selectId]);
		showScoreInfo();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == done) {
			Intent intent = new Intent(SelectPicture.this, AnimeWin.class);
			intent.putExtra("id", pictureResource[selectId]);
			startActivity(intent);
			finish();
		} else if (v == back) {
			finish();
		} else if (v == next) {
			next();
		} else if (v == prev) {
			prev();
		}
	}
}
